package com.lebang.controller.sys.rouseday;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.RouseDayMemberRecord;
import com.lebang.example.RouseDayMemberRecordExample;
import com.lebang.service.rouseday.RouseDayMemberRecordService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * rouse_day_member_record
 *
 * @author hwq
 * @date 2020-06-11
 */
@Controller
@RequestMapping("/sys/rousedaymemberrecord")
public class RouseDayMemberRecordController {
    @Autowired
    private RouseDayMemberRecordService baseService;

    @GetMapping("/list")
    public String list() {
        return "/rouseday/rousedaymemberrecord/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model) {
        RouseDayMemberRecord vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new RouseDayMemberRecord();
        }
        model.addAttribute("item", vo);
        return "/rouseday/rousedaymemberrecord/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(RouseDayMemberRecord vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }

    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        RouseDayMemberRecordExample example = new RouseDayMemberRecordExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        RouseDayMemberRecordExample.Criteria criteria = example.createCriteria();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
}
